package ar.edu.uba.fi.jmit.monitorengine.backend.client.controller;

import java.util.List;

import ar.edu.uba.fi.jmit.monitorengine.backend.client.EventService;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.EventServiceAsync;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.Monitorengine;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.RecivedEventsPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.EventDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ReceivedMessagesController {
	
	private static int PAGE_SIZE = 15;
	
	private Monitorengine mainModule;
	
	private RecivedEventsPanel panel;
	
	private EventServiceAsync eventService = GWT.create(EventService.class);
	
	private int showingPage = 0;
	
	public ReceivedMessagesController(Monitorengine mainModule) {
		this.mainModule = mainModule;
		this.panel = mainModule.getRecivedMessagesPanel();
		this.panel.setController(this);
	}
	
	public void showRecivedMessages() {
		showRecivedMessages(showingPage);
	}
	
	public void showNextPage() {
		GWT.log("Showing Next Page");
		eventService.getReceivedEventsCount(new AsyncCallback<Long>() {

			@Override
			public void onFailure(Throwable arg0) {
				GWT.log("Could not get Received events count", arg0);
			}

			@Override
			public void onSuccess(Long totalNumber) {
				if(showingPage < getPageCount(totalNumber)) {
					showRecivedMessages(++showingPage);
				}
			}
		});
	}
	
	public void showPreviosPage() {
		GWT.log("Showing Previous Page");
		if(showingPage > 0) {
			showRecivedMessages(--showingPage);
		}
	}
	
	public void showFirstPage() {
		GWT.log("Showing First Page");
		showingPage = 0;
		showRecivedMessages(showingPage);
	}
	
	public void showLastPage() {
		GWT.log("Showing Last Page");
		eventService.getReceivedEventsCount(new AsyncCallback<Long>() {

			@Override
			public void onFailure(Throwable arg0) {
				GWT.log("Could not get Received events count", arg0);
			}

			@Override
			public void onSuccess(Long totalNumber) {
				showingPage = getPageCount(totalNumber);
				showRecivedMessages(showingPage);
			}

		});
		
	}

	public void showRecivedMessages(int page) {
		GWT.log("Showing recived messages, page " + page);
		this.showingPage = page;
		eventService.getLastRecivedEvents(page * PAGE_SIZE, (page + 1) * PAGE_SIZE, new AsyncCallback<List<EventDTO>>() {
			
			@Override
			public void onSuccess(List<EventDTO> events) {
				mainModule.showRecivedMessagesPanel();
				panel.showEvents(events);
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				//mostrar error
				GWT.log("Could not get last sended events", arg0);
			}
		});
		
		eventService.getReceivedEventsCount(new AsyncCallback<Long>() {

			@Override
			public void onFailure(Throwable arg0) {
				GWT.log("Could not get Received events count", arg0);
			}

			@Override
			public void onSuccess(Long totalNumber) {
				panel.showPagination(getPageCount(totalNumber), showingPage);
			}

		});
		
	}
	
	private Integer getPageCount(Long totalNumber) {
		return (int)(totalNumber / (long)PAGE_SIZE);
	}
	
	

}
